package com.zhss.dsf.client;

import com.zhss.dfs.namenode.rpc.model.MkdirRequest;
import com.zhss.dfs.namenode.rpc.model.MkdirResponse;
import com.zhss.dfs.namenode.rpc.model.ShutdownRequest;
import com.zhss.dfs.namenode.rpc.service.NameNodeServiceGrpc;
import io.grpc.ManagedChannel;
import io.grpc.netty.NegotiationType;
import io.grpc.netty.NettyChannelBuilder;

public class FileSystemImpl implements FileSystem {
    private NameNodeServiceGrpc.NameNodeServiceBlockingStub namenode;
    private static final String NAMENODE_HOSTNAME = "localhost";
    private static final Integer NAMENODE_PORT = 50070;

    public FileSystemImpl() {
        ManagedChannel channel = NettyChannelBuilder
                .forAddress(NAMENODE_HOSTNAME, NAMENODE_PORT)
                .negotiationType(NegotiationType.PLAINTEXT)
                .build();
        this.namenode = NameNodeServiceGrpc.newBlockingStub(channel);
    }

    @Override
    public void mkdir(String path) {
        MkdirResponse response = namenode.mkdir(MkdirRequest.newBuilder().setPath(path).build());
    }

    @Override
    public void shutdown() {
        namenode.shutdown(ShutdownRequest.newBuilder().setCode(1).build());
    }
}
